﻿/*******************************jquery-plugin******************************/
$.fn.lavaLamp = function(o) {
    o = $.extend({ fx: "linear", speed: 500, click: function(){} }, o || {});

    return this.each(function() {
        var me = $(this), noop = function(){},
            $back = $('<li class="back"><div class="left"></div></li>').appendTo(me),
            $li = $("li:not('.sub_nav li')", this), curr = $("li:not('.sub_nav li').current", this)[0] || $($li[0]).addClass("current")[0];
			
        $li.not(".back").hover(function() {
            move(this);
        }, noop);

        $(this).hover(noop, function() {
            move(curr);
        });

        $("li", this).click(function(e) {
            setCurr(this);
            return o.click.apply(this, [e, this]);
        });

        setCurr(curr);

        function setCurr(el) {
            $back.css({ "left": el.offsetLeft+"px", "width": el.offsetWidth+"px" });
            curr = el;
        };

        function move(el) {
            $back.each(function() {
                $.dequeue(this, "fx"); }
            ).animate({
                width: el.offsetWidth,
                left: el.offsetLeft
            }, o.speed, o.fx);
        };

    });
};

//popup plugin start
jQuery.fn.popup = function(opts){
	var defaults = {
		num:0
	};
	var opts = jQuery.extend(defaults,opts);
	return this.live('click',function(){
	remove_shadow();
	var shadow = '<div class="shadow_bg"></div>';				   
	$('body').prepend(shadow);
	shadow_pic();
	$('.shadow_bg').live('click',function(){remove_shadow()});
	$('.popup .popup_box:eq('+opts.num+')').show().siblings('.popup_box').hide();
	$('.popup .vml_border_radius:eq('+opts.num+')').css('left',$('.popup .popup_box:eq('+opts.num+')').offset().left+'px').show().siblings('.vml_border_radius').hide();
	$('.popup .close').live('click',function(){remove_shadow()});	
	$(window).resize(function(){shadow_pic()});
	return false;
	});
}
var flag_shadow = false;
//remove shadow
function remove_shadow(){
		$('.shadow_bg').remove();
		$('.popup').children().hide();
		flag_shadow = false;
}
//acculate shadow size
function shadow_pic(){
	var bg_aw = $('body').width();
	var bg_ah = $('body').height();
	var win_w = $(window).width();
	var win_h = $(window).height();
	var end_w = win_w<bg_aw?bg_aw:win_w;
	var end_h = win_h<bg_ah?bg_ah:win_h;	
	$('.shadow_bg').css({width:end_w+'px',height:end_h+'px'});
	flag_shadow = true;
}


/*******************************DOM load start******************************/
$(function(){
//reset content height
$(window).resize(function(){fill_blank();content_width();$('.sub_nav').width(1071);if(flag_shadow){shadow_pic();}});
$('.popup').children().hide();
//fill content blank
function fill_blank(){
	var blank = $(window).height()-$('body').height();
	var main_h = $('.main').height();
	if($('.left_panel').height()<$('.content').height())
	{	
		$('.left_panel').height($('.content').height()+41);
	}
	else
	{	
		$('.left_panel').css('min-height',main_h+blank-2+'px');
	}
	$('.main').css('min-height',main_h+blank+'px');
	//nav width
	//$('.nav').width($('.crumbs').width()+8);
	//$('.sub_nav_bg').width($('.crumbs').width()+7);
}
//popup
$('#btnSave2').popup({num:0});
$('#btnCancel2').popup({num:1});
$('#btnSave1').popup({num:2});
$('#btnCancel1').popup({num:3});

//tr hover
$('tbody tr').mouseover(function(){
	$(this).addClass('current').siblings('tr').removeClass('current');
})
//language
$('.lang').hover(
function(){$(this).children('ul').stop(true,true).slideDown('800');},
function(){$(this).children('ul').slideUp('800');}
)
//nav
var sub_num = $('.main_nav>li.current').prevAll('.main_nav>li').size();
sub_position();
$('.main_nav').lavaLamp({
                speed: 300,
                click: function(event, menuItem) {
					$(menuItem).not('.sub_nav li a').addClass('current').children('.sub_nav').show().end().siblings('li').removeClass('current').children('.sub_nav').hide();
                    sub_num = $('.main_nav>li.current').prevAll('.main_nav>li').size();
					window.location.href = $(this + ' a').attr('href');
					return false;
                }
});
$('.main_nav>li').live('mouseover',function(){
	$(this).siblings('li').children('a').removeClass('lia').addClass('color_white');
	$(this).children('a').removeClass('color_white').addClass('lia');
	$(this).children('.sub_nav').show().end().siblings('li').children('.sub_nav').hide();
});
$('.main_nav>li').live('mouseleave',function(){
	$(this).children('a').removeClass('lia');
	$(this).siblings('li').children('a').removeClass('color_white');
});
$('.nav').live('mouseleave',function(){
	$('.main_nav>li:eq('+sub_num+')').addClass('current').children('.sub_nav').show().end().siblings('li').removeClass('current').children('.sub_nav').hide();
});
$('.main_nav>li.current').trigger('mouseover');
//correct sub_nav position
function sub_position(){
	var con_win = $('.nav').offset().left;
	$('.main_nav>li').each(function(){
		if($(this).children().is('.sub_nav'))
		{	
			var each_win = $(this).children('.sub_nav').offset().left;
			var change = con_win-each_win;
			$(this).children('.sub_nav').css('left',change+'px');
		}
	})
}

//let_panel_title first reset
var corner = '<span class="corner"></span>';
$('.left_panel_title:first').prepend(corner).siblings('.left_panel_title').css({'background':'none','border':'0 none'});
//left_panel drag
$(".left_panel").resizable({
minWidth : 15,
maxWidth : 500,
handles :'e',
resize : function() {
	content_width();
	$(".left_panel").addClass('left');
}
});
fill_blank();
content_width();
$(window).trigger('resize');

function content_width(){
	var main_width = $(".main").width();
	var left_min = $(".left_panel").width()+44;
	$(".content").width(main_width-left_min-15);//-15表示滚动条的宽度。
}

//Click to toggle sidebar
$("#shrink").toggle(
	function(){
		var con_width = $(".main").width() - 50;
		$(".left_panel").css("overflow","hidden");
		$(".left_panel").animate({width: '7px'});
		$(".content").animate({ width: con_width},
			{complete: 
				function(){
					if($(".left_panel").position().left > 0){
						$("#shrink").removeClass('right rightIcon').addClass('leftIcon').css({'left':'-2px'});
					}else{
						$("#shrink").removeClass('right leftIcon').addClass('rightIcon').css({'right':'-3px'});
					}
					$(".left_panel").resizable({ disabled: true });
					$(".main").sortable({ disabled: true});
					$(".nav_wrapL").css("cursor","auto");
					$('.left_panel a').attr("disabled","disabled");
					$('.ui-resizable-e,.ui-resizable-w').css("cursor","auto");
				}
			}
		);
	},
	function(){
		var con_width = $(".main").width() - 260;
		$(".left_panel").css("overflow","visible");
		$(".left_panel").animate({width: '215px'});
		$(".content").animate({ width: con_width},
			{complete: 
				function(){
					if($(".left_panel").position().left > 0){
						$("#shrink").removeClass('right leftIcon').addClass('rightIcon').css({'left':'-4px'});
					}else{
						$("#shrink").addClass('right').removeClass('rightIcon').addClass('leftIcon');
					}
					$(".left_panel").resizable({ disabled: false });
					$(".main").sortable({ disabled: false});
					$(".nav_wrapL").css("cursor","move");
					$('.left_panel a').removeAttr("disabled");
					$('.ui-resizable-e,.ui-resizable-w').css("cursor","e-resize");
				}
			}
		);
	}
);
//move sidebar
$(".main").sortable(
{
	axis: 'x', revert: true, tolerance: 'intersect', cursor: 'move', handle: '.nav_wrapL',cancel: 'a',
	start: function(event,ui){
			$(".left_panel").removeClass('left');
			var width_lp = ui.item.width();
			var width_ph = $(".ui-sortable-placeholder").width();
			var w = width_lp < width_ph ? width_lp-15 : width_lp;
			$(".ui-sortable-placeholder").css({"visibility":"visible","width":w});
	},
	update: function(event,ui){
			if(ui.item.position().left > 0){
				$(".ui-resizable-handle").removeClass('ui-resizable-e').addClass('ui-resizable-w');
				$(".shrink").removeClass('right leftIcon').addClass('rightIcon').css({'left':'-4px'});
				//$(".nav_wrapL li").addClass('right_li');
				//$(".nav_wrapL .first").removeClass('first').addClass('right_first');
			}else{
				$(".ui-resizable-handle").addClass('ui-resizable-e').removeClass('ui-resizable-w');
				$(".shrink").removeClass('rightIcon').addClass('leftIcon').css({'right':'-4px','left':''});
				//$(".nav_wrapL li").removeClass('right_li');
				//$(".nav_wrapL .right_first").removeClass('right_first').addClass('first');
			}
	}
});

$("#filter").click(function(){
	$(".filter_list").slideToggle("fast");
	 event.stopPropagation();
});

//nav_wrapl click.
$('ul.nav_wrapL>li').click(function(){
	var i = $(this).index();
	$(this).addClass('current').siblings().removeClass('current');
	$('.left_panel_con>ul:eq('+i+')').removeClass('hidden').siblings().addClass('hidden');
});
//end js

$("#check_all").click(
	function(){
		$("input[name='check_list']").attr("checked",this.checked);
});

})

//click and dblclick binder
var url;
function click_binder(cnum,clickUrl,dbClickUrl){ 
	if(cnum==1){
		url = clickUrl;
		window.setTimeout("location=url;",500);
	}
	if(cnum==2){
		url = dbClickUrl;
		location=dbClickUrl;
	}
} 

function choose (checkBoxId, option1, option2, inputBoxId) {
	var option = document.getElementById(checkBoxId).value;
	if (option==option1) {
		document.getElementById(inputBoxId).disabled = false;
	} else if(option==option2) {
		document.getElementById(inputBoxId).disabled = true;
		document.getElementById(inputBoxId).value = "";
	}
}

function linkTo(url) {
	if(url!=""){
		window.location.href=url;
	}	
}

